package process;

import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.junit.Test;

public class Flink_Process_wordcount {

    //TODO 压平操作
    public static class MyFlatMapProcess extends ProcessFunction<String, String> {

        @Override
        public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
            String[] words = value.split(" ");
            for (String word : words) {
                out.collect(word);
            }
        }
    }

    //TODO 转换kv
    public static class MyMapProcess extends ProcessFunction<String, Tuple2<String, Integer>> {

        @Override
        public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
            out.collect(new Tuple2<>(value, 1));
        }
    }

    //TODO 累加count
    public static class MySumProcess extends KeyedProcessFunction<Tuple, Tuple2<String, Integer>, Tuple2<String, Integer>> {
        //状态声明
        private ValueState<Integer> countState;

        @Override
        public void open(Configuration parameters) throws Exception {
            //状态初始化
            countState = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("word_count", Integer.class, 0));
        }

        @Override
        public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
            //获取状态
            Integer lastCount = countState.value();
            //更新状态
            countState.update(lastCount + value.f1);

            //累加count并写出
            out.collect(new Tuple2<>(value.f0, countState.value()));

        }
    }


    @Test
    public void test() throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment().setParallelism(1);
        env.readTextFile("input/1.txt")
                .process(new ProcessFunction<String, String>() {
                    @Override
                    public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
                        for (String s : value.split(" ")) {
                            out.collect(s);
                        }
                    }
                })
                .process(new ProcessFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public void processElement(String value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                        out.collect(new Tuple2<>(value, 1));
                    }
                })
                .keyBy(value -> value.f0)
                .process(new KeyedProcessFunction<String, Tuple2<String, Integer>, Tuple2<String, Integer>>() {
                    private ValueState<Integer> sum;

                    @Override
                    public void open(Configuration configuration) throws Exception{
                        sum = getRuntimeContext().getState(new ValueStateDescriptor<Integer>("word", Integer.class));
                    }

                    @Override
                    public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                        Integer temp = sum.value();
                        if (temp != null) {
                            sum.update(temp + value.f1);
                        }else{
                            sum.update(value.f1);
                        }
                        out.collect(new Tuple2<>(value.f0,sum.value()));
                    }
                })
                .print();
        env.execute();
    }
}
